//
//  FLPresentationAnimator.swift
//  swift_learning
//
//  Created by aron on 2017/1/25.
//  Copyright © 2017年 arons. All rights reserved.
//

import UIKit

class FLPresentationAnimator: NSObject {
    
    // MARK:- 对外提供属性
    var presentedFrame : CGRect = CGRect.zero
    var presentCallback : ((_ presented : Bool) -> ())?
    
    var isPresented : Bool = false
}

// MARK: - ......::::::: transit delegate handler :::::::......
extension FLPresentationAnimator : UIViewControllerTransitioningDelegate {
    
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = true
        if (presentCallback != nil) {
            presentCallback!(isPresented);
        }
        
        return self
    }
    
    
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {

        isPresented = false
        if (presentCallback != nil) {
            presentCallback!(isPresented);
        }
        
        return self
    }
    
    // 定制弹出视图的尺寸以及自定义背景、点击事件
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        let presentationController = FLPresentationController(presentedViewController: presented, presenting: presenting)
        presentationController.presentedFrame = presentedFrame
        return presentationController
    }
    
}

extension FLPresentationAnimator : UIViewControllerAnimatedTransitioning {
    
    // 设置动画的时间
    public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.34
    }
    
    // 设置动画的效果
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        if isPresented {
            handlePresentAnim(transitionContext)
        }else {
            handleDismissAnim(transitionContext)
        }
    }
    
    // 处理弹出动画
    private func handlePresentAnim(_ transitionContext: UIViewControllerContextTransitioning) {
        let presentedView = transitionContext.view(forKey: .to)
        transitionContext.containerView.addSubview(presentedView!)
        
        presentedView?.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        presentedView?.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.0)
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            presentedView?.transform = CGAffineTransform.identity
        }) { (result) in
            transitionContext.completeTransition(true)
        }
    }
    
    // 处理弹出动画
    private func handleDismissAnim(_ transitionContext: UIViewControllerContextTransitioning) {
        let dismissView = transitionContext.view(forKey: .from)
        
        dismissView?.transform = CGAffineTransform.identity
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            dismissView?.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.0001)
        }) { (result) in
            transitionContext.completeTransition(true)
            dismissView?.removeFromSuperview()
        }
    }
}
